/**
 * Created by lee_mxue on 17/1/20.
 */
import React from "react";
import ReactDOM from "react-dom";
import AJAX from "../../lib/ajax";
import InfiniteScroll from "react-infinite-scroller";
import Rules from "../../lib/rules";
import { addErrorListener } from "../../lib/handleError"
import weixinShare from "../../lib/weixinShare";
require('../../lib/rem');
require('./SendingPackageHeader.less');
require('../../styles/common.less');
require('../../styles/resetWeui.less');
require('../../styles/lux.less')
const Toast = require('../MyPackage/components/Toast')


class App extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            isReady: false,     //是否加载页面
            goodsTextAreaIsOpen: false,  //选择物品输入框
            weightTextAreaIsOpen: false,  //选择物品重量输入框
            paymentTextAreaIsOpen: false,  //选择付款方式输入框
            actionSheetIsOpen: false,   //ActionSheet
            goodsKindIsOpen: false,      //选择物品
            goodsWeightIsOpen: false,   //选择物品重量
            addedServicesIsOpen: false,      //选择增值服务
            selectDeliveryIsOpen: false,      //选择快递公司
            selectPaymentIsOpen: false,      //选择付款方式
            agreementIsOpen: true,      //协议是否同意
            orderHomeInfo: {
                activity_msg: null, //str 寄件优惠信息描述，如果没有，此项为null
                sender_addr: null, //最近一次寄件地址信息，如果没有，此项为null
                nearest_logis: null, //最近物流中心
                last_express: null, //最近一次寄件的快递公司，如果没有，此项为null
            },  //国内寄件首页信息
            selectExpress: undefined,      //已选快递公司
            selectSite: "请选择服务站点",  //已选站点名称
            selectSiteId: 0,  //已选站点Id
            selectReceiverAddress: "请选择收件人详细地址",  //已选收货人地址
            selectReceiverInfo: undefined,  //已选收货人信息
            destinationProvinceCode: "", //目的地省份编码
            selectGoodName: '请选择物品', //选择物品名称
            selectGoodName_id: 0, //选择物品id
            selectGoodWeight: 0, //选择物品重量
            selectGoodWeight_id: 0, //选择物品重量id
            selectGoodPaymentId: 1, //物品付款方式id  默认寄件人付
            selectGoodPaymentName: "寄件人付", //物品付款方式name  默认寄件人付
            vipCode: "", //VIP卡号，当pay_side为3时，必选；否则可以为空
            //－－－－－－－－－－－－－－－－－－增值服务（保价）－－－－－－－－－－－－－－－－－－
            showInsureFee: undefined, //保价显示
            selectGoodPrice: 0, //选择物品价格(保价估值)
            insureFee: 0,      //保价费用
            calc_fee: 0,      //订单总费用
            //－－－－－－－－－－－－－－－－－－快递公司－－－－－－－－－－－－－－－－－－
            current: 1, //快递公司－－－当前页码
            loadCount: 1,    //快递公司－－－加载次数
            sortby: 1,      //快递公司－－－列表排序方式
            expressCount: 0,   //列表总数
            expressList: []    //快递公司列表

        };
        this.switchGoodsTextarea = this.switchGoodsTextarea.bind(this);
        this.switchWeightTextarea = this.switchWeightTextarea.bind(this);
        this.switchPaymentTextarea = this.switchPaymentTextarea.bind(this);
        this.getInputValue = this.getInputValue.bind(this); //获取输入框value
        this.switchSiteList = this.switchSiteList.bind(this);
        this.handleSelectGood = this.handleSelectGood.bind(this);   //选择物品
        this.handleSelectGoodList = this.handleSelectGoodList.bind(this);   //选择物品列表
        this.handleSelectWeightList = this.handleSelectWeightList.bind(this);   //选择物品重量列表
        this.handleSelectWeight = this.handleSelectWeight.bind(this);   //选择物品重量
        this.getOrderFeeList = this.getOrderFeeList.bind(this);   //获取快递公司
        this.handleSelectExpress = this.handleSelectExpress.bind(this);   //选择快递公司
        this.handleGetDefaultExpress = this.handleGetDefaultExpress.bind(this);   //获取默认快递公司
        this.getExpenses = this.getExpenses.bind(this);   //获取保价费用
        this.handleConformInsureFee = this.handleConformInsureFee.bind(this);   //确认保价费用
        this.handleSelectPayType = this.handleSelectPayType.bind(this);   //选择寄件方式
        this.handleMatchVip = this.handleMatchVip.bind(this);   //校验匹配VIp
        this.handleSwitchAgree = this.handleSwitchAgree.bind(this);   //同意协议
        this.handleSubmit = this.handleSubmit.bind(this);   //提交寄件单
    }

    componentWillMount() {
        addErrorListener()()
        weixinShare.hideAll();
    }


    componentDidMount() {
        var _this = this;
        AJAX.appendBaiduAnalysis()
        //获取签名
        AJAX.request("get_signature", {
            full_url: window.location.href
        }, (data) => {
            wx.config({
                debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                appId: data.body.appid, // 必填，公众号的唯一标识
                timestamp: data.body.timestamp, // 必填，生成签名的时间戳
                nonceStr: data.body.noncestr, // 必填，生成签名的随机串
                signature: data.body.signature,// 必填，签名，见附录1
                jsApiList: ["getLocation"] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
            })
            wx.ready(()=> {
                _this.setState({
                    isReady: true,     //是否加载页面
                })
                //获取地理位置
                wx.getLocation({
                    type: 'wgs84', // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'
                    success: function (res) {
                        var latitude = res.latitude; // 纬度，浮点数，范围为90 ~ -90
                        var longitude = res.longitude; // 经度，浮点数，范围为180 ~ -180。
                        var speed = res.speed; // 速度，以米/每秒计
                        var accuracy = res.accuracy; // 位置精度

                        //存储用户地理位置
                        window.localStorage.removeItem('sender_locationInfo_x');
                        window.localStorage.sender_locationInfo_x = JSON.stringify({
                            latitude: latitude,   //纬度
                            longitude: longitude,   //经度
                        })

                        //获取寄件首页信息
                        AJAX.request("order_home", {
                            latitude: latitude,   //纬度
                            longitude: longitude,   //经度
                        }, (data) => {

                            if (data.code == 0) {
                                //国内寄件首页信息
                                var myOrderHomeInfo = {
                                    activity_msg: data.body.activity_msg,   //str 寄件优惠信息描述，如果没有，此项为null
                                    sender_addr: data.body.sender_addr,     //最近一次寄件地址信息，如果没有，此项为null
                                    nearest_logis: data.body.nearest_logis, //最近物流中心
                                    last_express: data.body.last_express,   //最近一次寄件的快递公司，如果没有，此项为null
                                };

                                //将国内寄件首页信息－默认信息存入localstorage
                                _this.setState({
                                    orderHomeInfo: myOrderHomeInfo
                                })

                                //－－－－－－－－－－－－－－－－－－获取localStorage－－－－－－－－－－－－－－－－－－
                                //站点信息---物流中心
                                var logisInfo = JSON.parse(localStorage.getItem('logisInfo_x'));
                                // localStorage.removeItem('logisInfo_x');

                                var sender_addr = JSON.parse(localStorage.getItem('sender_addr_x'));
                                // localStorage.removeItem('sender_addr_x');

                                //如果没有选择服务站点－－－设置默认
                                // alert(logisInfo)
                                if (!logisInfo) {
                                    //设置默认物流中心
                                    window.localStorage.logisInfo_x = JSON.stringify(myOrderHomeInfo.nearest_logis)
                                    _this.setState({
                                        selectSite: myOrderHomeInfo.nearest_logis.name,
                                        selectSiteId: myOrderHomeInfo.nearest_logis.id,
                                    })
                                } else {
                                    _this.setState({
                                        selectSite: logisInfo.name,
                                        selectSiteId: logisInfo.id,
                                    })
                                }

                                //如果没有填写寄件地址－－－设置默认
                                if (!sender_addr) {
                                    //设置默认物流中心
                                    window.localStorage.sender_addr_x = JSON.stringify(myOrderHomeInfo.sender_addr)
                                }
                            }
                        })
                        _this.setState({
                            latitude: latitude,   //纬度
                            longitude: longitude,   //经度
                        })
                    }
                });
            })

            //获取localStorage－－－站点信息
            // 站点信息---物流中心
            // var logisInfo_x = JSON.parse(localStorage.getItem('logisInfo_x'));
            // if (logisInfo_x) {
            //     this.setState({
            //         selectSite: logisInfo_x.name,
            //         selectSiteId: logisInfo_x.id,
            //     })
            // }

            // 收件人地址
            var receiverData = JSON.parse(localStorage.getItem('receiverData_x'));
            if (receiverData) {
                this.setState({
                    selectReceiverInfo: receiverData,
                    selectReceiverAddress: receiverData.province + receiverData.city + receiverData.county,
                })
            }
            //物品信息---已选物品名称
            var sendingPackageHeader_goodName_x = JSON.parse(localStorage.getItem('sendingPackageHeader_goodName_x'));
            if (sendingPackageHeader_goodName_x) {
                this.setState({
                    selectGoodName: sendingPackageHeader_goodName_x.selectGoodName,
                    selectGoodName_id: sendingPackageHeader_goodName_x.selectGoodName_id
                })
            }
            //物品信息---已选物品重量
            var sendingPackageHeader_goodWeight_x = JSON.parse(localStorage.getItem('sendingPackageHeader_goodWeight_x'));
            if (sendingPackageHeader_goodWeight_x) {
                this.setState({
                    selectGoodWeight: sendingPackageHeader_goodWeight_x.selectGoodWeight,
                    selectGoodWeight_id: sendingPackageHeader_goodWeight_x.selectGoodWeight_id
                })
            }
            //已选快递公司
            var sendingPackageHeader_expressInfo_x = JSON.parse(localStorage.getItem('sendingPackageHeader_expressInfo_x'));
            if (sendingPackageHeader_expressInfo_x) {
                this.setState({
                    selectExpress: sendingPackageHeader_expressInfo_x.selectExpress,
                    calc_fee: sendingPackageHeader_expressInfo_x.calc_fee       //分
                })
            }

            //默认快递公司（无已选快递公司）
            var sendingPackageHeader_defaultExpress_x = JSON.parse(localStorage.getItem('sendingPackageHeader_defaultExpress_x'));
            if (!sendingPackageHeader_expressInfo_x) {
                if (sendingPackageHeader_defaultExpress_x) {
                    this.setState({
                        selectExpress: sendingPackageHeader_defaultExpress_x.selectExpress,
                        calc_fee: sendingPackageHeader_defaultExpress_x.calc_fee       //分
                    })
                }
            }

            //已选支付方式
            var sendingPackageHeader_payType_x = JSON.parse(localStorage.getItem('sendingPackageHeader_payType_x'));
            if (sendingPackageHeader_payType_x) {
                this.setState({
                    selectGoodPaymentId: sendingPackageHeader_payType_x.selectGoodPaymentId,    //已选寄件方式id
                    selectGoodPaymentName: sendingPackageHeader_payType_x.selectGoodPaymentName     //已选寄件方式name
                })
            }

            //已填VIP信息
            var sendingPackageHeader_vipCode_x = JSON.parse(localStorage.getItem('sendingPackageHeader_vipCode_x'));
            //付款方式为寄件月结
            if (sendingPackageHeader_payType_x && sendingPackageHeader_payType_x.selectGoodPaymentId == 3) {
                if (sendingPackageHeader_vipCode_x) {
                    this.setState({
                        vip_code: sendingPackageHeader_vipCode_x.vip_code,        //分
                    })
                }
            }

            //已填写保价信息
            var sendingPackageHeader_insureFee_x = JSON.parse(localStorage.getItem('sendingPackageHeader_insureFee_x'));
            if (sendingPackageHeader_insureFee_x) {
                this.setState({
                    showInsureFee: sendingPackageHeader_insureFee_x.showInsureFee,    //保价显示
                    insureFee: sendingPackageHeader_insureFee_x.insureFee,        //分
                    calc_fee: sendingPackageHeader_insureFee_x.calc_fee      //分
                })
            }
        })
    }

    //toast弹窗
    warn(content) {
        Toast.warn({
            content: content
        })
    }

    success() {
        Toast.success({
            content: "成功"
        })
    }

    error() {
        Toast.error({
            content: "失败"
        })
    }

    loading() {
        Toast.loading({
            content: "数据加载中"
        })
    }

    //ActionSheet
    switchSiteList(isOpen, type = "actionSheetIsOpen") {
        var _this = this;
        //重置
        this.setState({
            goodsTextAreaIsOpen: false,  //选择物品输入框
            weightTextAreaIsOpen: false,  //选择物品重量输入框
            paymentTextAreaIsOpen: false,  //选择付款方式输入框
            goodsWeightIsOpen: false,   //选择物品重量
            goodsKindIsOpen: false,      //选择物品
            addedServicesIsOpen: false,      //选择增值服务
            selectDeliveryIsOpen: false,      //选择快递公司
            selectPaymentIsOpen: false,      //选择付款方式
        })
        //------------------开弹窗 isOpen = true --------------
        //快递公司
        if (type == "selectDeliveryIsOpen") {
            //目的地省份编码
            if (this.state.selectReceiverInfo) {
                //获取快递公司
                _this.getOrderFeeList(1, 1);
            } else {
                this.warn("请先选择收货地址")
            }
        } else {
            this.setState({
                actionSheetIsOpen: isOpen,
                [type]: isOpen
            })
        }
        //增值服务（保价）
        if (type == "addedServicesIsOpen" && !this.state.selectExpress) {
            this.warn("请先选择快递公司");
        }

        //------------------关弹窗 isOpen = false --------------
    }

    //加载更多(快递公司)
    loadMoreData(current) {
        // alert(current)
        var _current = current + 1;
        var _this = this;
        //判断加载次数
        if (this.state.loadCount) {
            if (_current < _this.state.loadCount || _current == _this.state.loadCount) {
                // alert("111！！！")
                _this.getOrderFeeList(_this.state.sortby, _current)
            } else {
                // alert("没有数据了！！！")
            }
        }
    }

    //获取快递公司列表（ 快递公司id, 保价估值, 排序方式－－－1.综合（利润）2.价格3.速度，默认1）
    getOrderFeeList(sortby, current) {
        // alert(current)
        var _this = this;
        this.setState({
            current: current,
            sortby: sortby,
        })
        AJAX.request("order_fee_list", {
            page: current,
            psize: 5,
            sortby: sortby,         //排序方式
            logis_id: this.state.selectSiteId,   //物流中心id
            province_code: this.state.selectReceiverInfo.province_code,   //目的省份编码
            city_code: this.state.selectReceiverInfo.city_code,   //目的城市编码
            county_code: this.state.selectReceiverInfo.county_code,   //目的区县编码
            weight: this.state.selectGoodWeight * 1000,   //包裹重量
        }, (data) => {
            if (data.code == 0) {
                let myExpressListData = {
                    count: data.body.count,
                    list: data.body.list
                }
                // alert(JSON.stringify(myExpressListData))
                if (current == 1) {
                    _this.setState({
                        expressList: myExpressListData.list,
                    })
                } else {
                    _this.setState({
                        expressList: _this.state.expressList.concat(myExpressListData.list),
                    })
                }
                _this.setState({
                    expressCount: myExpressListData.count,
                    loadCount: Math.ceil(myExpressListData.count / 5),    //获取总页数（psize == 5）
                    actionSheetIsOpen: true,
                    selectDeliveryIsOpen: true,
                    sortby: sortby
                })

            }
        })
    }

    //选择快递公司
    handleSelectExpress(item) {
        var _this = this;
        this.setState({
            //清空保价信息
            // showInsureFee: undefined, //保价显示
            // selectGoodPrice: 0, //选择物品价格(保价估值)(分)
            // insureFee: 0,      //保价费用
            selectExpress: item,    //快递公司对象
        })

        //如果设置保价－－－重新获取保价
        if (this.state.insureFee != 0) {
            AJAX.request("order_get_insure_fee", {
                evaluation: parseInt(this.state.selectGoodPrice * 100),   //保价估值
                tariff_id: item.tariff_id,   //资费id
            }, (data) => {
                if (data.code == 0) {
                    var myInsureFee = data.body.insure_fee;
                    // alert(myInsureFee)
                    _this.setState({
                        insureFee: myInsureFee,     //(分)
                        showInsureFee: myInsureFee, //保价显示
                        calc_fee: item.amount + myInsureFee        //订单总费用 分
                    })
                    //将已选快递公司存入localStorage------sendingPackageHeader_expressInfo_x
                    window.localStorage.sendingPackageHeader_expressInfo_x = JSON.stringify({
                        selectExpress: item,
                        calc_fee: item.amount + myInsureFee       //分
                    })
                    //将已填保价和总价存入localStorage------sendingPackageHeader_feeInfo_x
                    window.localStorage.sendingPackageHeader_insureFee_x = JSON.stringify({
                        insureFee: myInsureFee,     //(分)
                        showInsureFee: myInsureFee, //保价显示
                        calc_fee: item.amount + myInsureFee      //分
                    })
                }
            })
        } else {
            this.setState({
                calc_fee: item.amount         //订单总费用 分
            })
            //将已选快递公司存入localStorage------sendingPackageHeader_expressInfo_x
            window.localStorage.sendingPackageHeader_expressInfo_x = JSON.stringify({
                selectExpress: item,
                calc_fee: item.amount      //分
            })
        }

        this.switchSiteList(false)
    }

    //物品名称－－－其他选项输入框
    switchGoodsTextarea(id) {
        //    验证物品名称
        //当物品输入框关闭时
        if (this.state.goodsTextAreaIsOpen) {
            //输入框内文本不为空时
            if (this.state.selectGoodName.length != 0) {
                if (!Rules.len(this.state.selectGoodName, 1, 20)) {
                    this.warn("物品名称格式错误")
                } else {
                    //验证通过
                    this.setState({
                        goodsTextAreaIsOpen: !this.state.goodsTextAreaIsOpen,
                        selectGoodName_id: id
                    })
                    if (this.state.goodsTextAreaIsOpen == true) {
                        this.handleSelectGoodList()
                    }
                    //将已选物品存入localStorage------sendingPackageHeader_goodName_x
                    window.localStorage.sendingPackageHeader_goodName_x = JSON.stringify({
                        selectGoodName: this.state.selectGoodName,
                        selectGoodName_id: id
                    })

                }
            } else {
                this.warn("请输入物品名称")
            }
        } else {
            //当物品输入框打开时
            this.setState({
                selectGoodName: '',
                goodsTextAreaIsOpen: !this.state.goodsTextAreaIsOpen,
                selectGoodName_id: id
            })
            if (this.state.goodsTextAreaIsOpen == true) {
                this.handleSelectGoodList()
            }
        }
    }

    //物品重量－－－其他选项输入框
    switchWeightTextarea(id) {
        var weight = String(this.refs.inputWeight.value)
        //    验证物品名称
        //当物品输入框关闭时
        if (this.state.weightTextAreaIsOpen) {
            //输入框内文本不为空时
            if (weight.length != 0) {
                if (!Rules.payMoney(weight) || weight > 1000) {
                    this.warn("物品重量格式错误")
                } else {
                    //验证通过
                    //当物品输入框打开时
                    this.setState({
                        weightTextAreaIsOpen: !this.state.weightTextAreaIsOpen,
                        selectGoodWeight_id: id,
                    })
                    if (this.state.weightTextAreaIsOpen == true) {
                        this.handleSelectWeightList()
                        //    获取默认快递公司
                        this.handleGetDefaultExpress(weight)

                    }
                    //将已选重量存入localStorage------sendingPackageHeader_goodWeight_x
                    window.localStorage.sendingPackageHeader_goodWeight_x = JSON.stringify({
                        selectGoodWeight: weight,
                        selectGoodWeight_id: id
                    })
                }
            } else {
                this.warn("请输入物品重量")
            }
        } else {
            //当物品输入框打开时
            this.setState({
                weightTextAreaIsOpen: !this.state.weightTextAreaIsOpen,
                selectGoodWeight_id: id,
            })
            if (this.state.weightTextAreaIsOpen == true) {
                this.handleSelectWeightList()
                //    获取默认快递公司
                this.handleGetDefaultExpress(weight)

            }
        }
    }

    //付款方式－－－寄付月结输入框
    switchPaymentTextarea() {
        this.setState({
            paymentTextAreaIsOpen: !this.state.paymentTextAreaIsOpen
        })
    }

    //获取输入框value
    getInputValue(type, e) {
        var value = e.target.value;
        this.setState({
            [type]: value
        })
    }


    //选择物品---列表
    handleSelectGoodList() {
        this.setState({
            goodsWeightIsOpen: !this.state.goodsWeightIsOpen,
            goodsKindIsOpen: !this.state.goodsKindIsOpen,
            weightTextAreaIsOpen: false,  //选择物品重量输入框
        })
    }

    //选择物品
    handleSelectGood(pkg_type, id) {
        // console.log("pkg_type: ", pkg_type)
        this.setState({
            goodsTextAreaIsOpen: false, //物品输入框关闭
            selectGoodName: pkg_type,   //已选物品名称
            selectGoodName_id: id
        })

        //将已选物品存入localStorage------sendingPackageHeader_goodName_x
        window.localStorage.sendingPackageHeader_goodName_x = JSON.stringify({
            selectGoodName: pkg_type,
            selectGoodName_id: id
        })
        this.handleSelectGoodList()
    }

    //选择物品重量---列表
    handleSelectWeightList() {
        this.setState({
            goodsWeightIsOpen: false,
            goodsKindIsOpen: false,
            actionSheetIsOpen: false,
        })

    }

    //选择物品重量
    handleSelectWeight(weight, id) {
        this.setState({
            selectGoodWeight: weight,   //已选物品重量（kg）
            selectGoodWeight_id: id,
        })
        //将已选重量存入localStorage------sendingPackageHeader_goodWeight_x
        window.localStorage.sendingPackageHeader_goodWeight_x = JSON.stringify({
            selectGoodWeight: weight,
            selectGoodWeight_id: id
        })
        this.handleSelectWeightList()
        if (this.state.selectSiteId == 0) {
            this.warn("请选择服务站点")
        } else if (!this.state.selectReceiverInfo) {
            this.warn("请选择收件人")
        } else {
            this.handleGetDefaultExpress(weight)
        }
    }

    //获取默认快递公司
    handleGetDefaultExpress(weight) {
        var _this = this;

        AJAX.request("order_fee_list", {
            page: 1,
            psize: 1,
            logis_id: this.state.selectSiteId,   //物流中心id
            province_code: this.state.selectReceiverInfo.province_code,   //目的省份编码
            city_code: this.state.selectReceiverInfo.city_code,   //目的城市编码
            county_code: this.state.selectReceiverInfo.county_code,   //目的区县编码
            weight: weight * 1000,   //包裹重量
        }, (data) => {
            if (data.code == 0) {
                let myExpressListData = {
                    count: data.body.count,
                    list: data.body.list
                }
                _this.setState({
                    selectExpress: myExpressListData.list[0],
                })

                //如果设置保价－－－重新获取保价
                if (this.state.insureFee != 0) {
                    AJAX.request("order_get_insure_fee", {
                        evaluation: parseInt(this.state.selectGoodPrice * 100),   //保价估值
                        tariff_id: myExpressListData.list[0].tariff_id,   //资费id
                    }, (data) => {
                        if (data.code == 0) {
                            var myInsureFee = data.body.insure_fee;
                            // alert(myInsureFee)
                            _this.setState({
                                insureFee: myInsureFee,     //(分)
                                showInsureFee: myInsureFee, //保价显示
                                calc_fee: myExpressListData.list[0].amount + myInsureFee        //订单总费用 分
                            })
                            //将默认快递公司存入localStorage------sendingPackageHeader_feeInfo_x
                            window.localStorage.sendingPackageHeader_defaultExpress_x = JSON.stringify({
                                selectExpress: myExpressListData.list[0],
                                calc_fee: myExpressListData.list[0].amount + myInsureFee       //分
                            })
                            //将已填保价和总价存入localStorage------sendingPackageHeader_feeInfo_x
                            window.localStorage.sendingPackageHeader_insureFee_x = JSON.stringify({
                                insureFee: myInsureFee,     //(分)
                                showInsureFee: myInsureFee, //保价显示
                                calc_fee: myExpressListData.list[0].amount + myInsureFee         //分
                            })
                        }
                    })
                } else {
                    this.setState({
                        calc_fee: myExpressListData.list[0].amount    //分
                    })
                    //将默认快递公司存入localStorage------sendingPackageHeader_feeInfo_x
                    window.localStorage.sendingPackageHeader_defaultExpress_x = JSON.stringify({
                        selectExpress: myExpressListData.list[0],
                        calc_fee: myExpressListData.list[0].amount     //分
                    })

                }


            }
        })
    }

    //获取报价费用
    getExpenses(e) {
        var _this = this;
        var value = e.target.value;
        // console.log("物品价值： ", e.target.value)
        this.setState({
            selectGoodPrice: parseInt(value * 100)     //（保价估值／分）
        })
        if (!Rules.payMoney(value)) {
            this.warn("物品价值只能精确到两位小数")
        } else if (value > 10000000) {
            this.warn("物品价值不能>100万")
        } else {
            if (value !== "") {
                AJAX.request("order_get_insure_fee", {
                    evaluation: parseInt(value * 100),   //保价估值
                    tariff_id: this.state.selectExpress.tariff_id,   //资费id
                }, (data) => {
                    if (data.code == 0) {
                        var myInsureFee = data.body.insure_fee;
                        // alert(myInsureFee)
                        _this.setState({
                            insureFee: myInsureFee
                        })
                    }
                })
            }
        }


    }

    //确认保价费用
    handleConformInsureFee() {
        var calc_fee = this.state.insureFee + this.state.selectExpress.amount;     //总价（分）
        this.setState({
            showInsureFee: this.state.insureFee,    //保价显示
            actionSheetIsOpen: false,
            calc_fee: calc_fee    //费用估算（元）－－－总价显示(分)
        })
        //将已填保价和总价存入localStorage------sendingPackageHeader_feeInfo_x
        window.localStorage.sendingPackageHeader_insureFee_x = JSON.stringify({
            showInsureFee: this.state.insureFee,    //保价显示
            insureFee: this.state.insureFee,        //分
            calc_fee: calc_fee      //分
        })

        this.switchSiteList(false);
    }

    //选择寄件方式
    handleSelectPayType(id, name) {
        this.setState({
            selectGoodPaymentId: id,    //已选寄件方式id
            selectGoodPaymentName: name     //已选寄件方式name
        })
        //将已选付款方式存入localStorage------sendingPackageHeader_payType_x
        window.localStorage.sendingPackageHeader_payType_x = JSON.stringify({
            selectGoodPaymentId: id,    //已选寄件方式id
            selectGoodPaymentName: name     //已选寄件方式name
        })
        if (id == 3) {
            this.setState({
                paymentTextAreaIsOpen: true
            })
        } else {
            this.switchSiteList(false);
        }
    }

    //校验匹配Vip
    handleMatchVip() {
        var _this = this;
        var vip = this.refs.myVipNum.value;
        var regu = /^[0-9]*$/g;    //纯数字
        var re = new RegExp(regu);
        if (!this.state.selectExpress) {
            this.warn("请选择快递公司")
        } else {
            if (re.test(vip)) {
                AJAX.request("vip_info_check", {
                    vip_code: vip,   //vip卡号
                    express_id: this.state.selectExpress.id,   //快递公司id
                }, (data) => {
                    if (data.code == 0) {
                        _this.switchSiteList(false)
                        //将已输入VIP卡号存入localStorage------sendingPackageHeader_vipCode_x
                        window.localStorage.sendingPackageHeader_vipCode_x = JSON.stringify({
                            vip_code: vip,
                        })
                    } else {
                        this.refs.myVipNum.value = ''
                    }
                })
            } else {
                this.warn("输入格式有误");
                this.refs.myVipNum.value = ''
            }
        }

    }

    //同意协议
    handleSwitchAgree() {
        this.setState({
            agreementIsOpen: !this.state.agreementIsOpen
        })
    }

    //提交
    handleSubmit() {
        //获取收件人信息
        var consignee_addr = JSON.parse(localStorage.getItem("receiverData_x"))
        //获取寄件人信息
        var sender_addr = localStorage.getItem("sender_addr_x")
        //获取物流中心信息
        var logis_info = JSON.parse(localStorage.getItem("logisInfo_x"))
        //获取已选快递公司
        var selectExpress = this.state.selectExpress
        if (!selectExpress) {
            this.warn("请先选择快递公司")
        } else if (!JSON.parse(sender_addr)) {
            // alert(sender_addr);
            this.warn("请先填写寄件人信息")
        } else if (!logis_info) {
            this.warn("请先选择物流中心")
        } else if (!consignee_addr) {
            this.warn("请先选择收件人")
        } else if (!this.state.agreementIsOpen) {
            this.warn("不可提交")
            //    寄件月付必须填写VIP卡号
        } else if (this.state.selectGoodPaymentId == 3 && this.state.vipCode === '') {
            this.warn("请输入VIP卡号")
        } else {
            AJAX.request("order_save", {
                province_code: '',   //目的省份编码
                sender_addr: sender_addr,   //寄件人信息
                consignee_addr_id: consignee_addr.id,   //收件人地址id
                logis_id: logis_info.id,   //物流中心id
                pkg_type: this.state.selectGoodName,   //包裹名称
                weight: this.state.selectGoodWeight * 1000,   //包裹重量（整数，单位克)
                evaluation: this.state.selectGoodPrice,   //保价估值（整数，单位分），默认0   ［可选］
                insure_fee: this.state.insureFee,   //保价费用（整数，单位分），默认0     ［可选］
                express_fee: selectExpress.amount,   //快递费用（整数，单位分），(快递公司列表返回值中的amount，基础价格)
                express_id: selectExpress.id,   //快递公司id
                tariff_id: selectExpress.tariff_id,   //资费id
                pay_side: this.state.selectGoodPaymentId,   // 付款方式,1:寄付2:到付3:寄付月结
                vip_code: this.state.vipCode,   //VIP卡号，当pay_side为3时，必选；否则可以为空。
            }, (data) => {
                if (data.code == 0) {
                    window.localStorage.removeItem("order_info_x")
                    window.localStorage.order_info_x = JSON.stringify(data.body)
                    this.success();
                    window.location.href = "./OrderInfo"

                }

            })
        }
    }

    render() {
        // alert(this.state.actionSheetIsOpen)
        // console.log("weightTextAreaIsOpen: ",this.state.weightTextAreaIsOpen)
        // console.log("expressListData: ",this.state.expressListData)
        // console.log(" this.state.actionSheetIsOpen: ", this.state.actionSheetIsOpen)
        // alert(this.state.expressListData.list)
        const loader = <div className="loader"></div>
        const calc_fee = (this.state.calc_fee / 100).toFixed(2)
        // console.log("goodsTextAreaIsOpen: ", this.state.goodsTextAreaIsOpen);
        var goodsInputClassName = this.state.goodsTextAreaIsOpen ? "flex" : "none"; //物品名称－－－其他
        var weightInputClassName = this.state.weightTextAreaIsOpen ? "flex" : "none";   //物品重量－－－其他
        var paymentInputClassName = this.state.paymentTextAreaIsOpen ? "flex" : "none"; //支付方式－－－3 VIP
        var actionSheetClassName = this.state.actionSheetIsOpen ? " weui-actionsheet weui-actionsheet_toggle" : "weui-actionsheet"; //actionsheet
        var maskClassName = this.state.actionSheetIsOpen ? " weui-mask" : "weui-mask none"; //action sheet 遮罩层
        return (
            this.state.isReady &&
            <div className="main">
                {this.state.orderHomeInfo.activity_msg && <div className=" top_active">
                    <p className="of_scroll">{this.state.orderHomeInfo.activity_msg}</p>
                </div>}

                <div className="flex_column center_content">
                    <div className="content_top">
                        <div className=" weui-cells">
                            <a className="weui-cell weui-cell_access" href="./SendingPackageChooseSite?send_type=1">
                                <div className="weui-cell__hd">
                                    <img src="//wechat.jinlb.cn/assets/images/iconji.png" alt=""
                                         style={{width: 40, marginRight: 5, display: "block"}}/>
                                </div>
                                <div className="weui-cell__bd">
                                    <p>{this.state.selectSite}</p>
                                </div>
                                <div className="weui-cell__ft"></div>
                            </a>
                            <a className="weui-cell weui-cell_access" href="./SendingPackageReceiverAddress?send_type=1">
                                <div className="weui-cell__hd">
                                    <img src="//wechat.jinlb.cn/assets/images/iconshou.png" alt=""
                                         style={{width: 40, marginRight: 5, display: "block"}}/>
                                </div>
                                <div className="weui-cell__bd">
                                    <p>{this.state.selectReceiverAddress}</p>
                                </div>
                                <div className="weui-cell__ft"></div>
                            </a>
                        </div>
                    </div>
                    <div className="content_center ">
                        <div className="weui-cells">
                            <a className="weui-cell weui-cell_access"
                               onClick={this.switchSiteList.bind(this, true, "goodsKindIsOpen")}>
                                <div className="weui-cell__bd">
                                    <p>物品</p>
                                </div>
                                <div className="weui-cell__ft">
                                    <span>{this.state.selectGoodName}</span>
                                    <span>{this.state.selectGoodWeight ? this.state.selectGoodWeight + 'Kg' : ""}</span>
                                </div>
                            </a>
                            <a className="weui-cell weui-cell_access"
                               onClick={this.switchSiteList.bind(this, true, "addedServicesIsOpen")}>
                                <div className="weui-cell__bd">
                                    <p>增值服务</p>
                                </div>
                                <div
                                    className="weui-cell__ft">{!this.state.showInsureFee ? '选填' : '保价费' + this.state.showInsureFee / 100 + '元'}</div>
                            </a>
                        </div>
                    </div>
                    <div className="content_bottom">
                        <div className="weui-cells">
                            <div className="weui-cell">
                                <div className="weui-cell__bd">
                                    <p>寄件方式</p>
                                </div>
                                <div className="weui-cell__ft">服务站点自寄</div>
                            </div>
                            <a className="weui-cell weui-cell_access"
                               onClick={this.switchSiteList.bind(this, true, "selectDeliveryIsOpen")}>
                                <div className="weui-cell__bd">
                                    <p>快递公司</p>
                                </div>
                                <div
                                    className="weui-cell__ft">{this.state.selectExpress ? this.state.selectExpress.name : '请选择快递公司'}</div>
                            </a>
                            <a className="weui-cell weui-cell_access"
                               onClick={this.switchSiteList.bind(this, true, "selectPaymentIsOpen")}>
                                <div className="weui-cell__bd">
                                    <p>付款方式</p>
                                </div>
                                <div className="weui-cell__ft">{this.state.selectGoodPaymentName}</div>
                            </a>

                        </div>
                    </div>

                    <p className="agreement">
                        <i className={`iconfont ${!this.state.agreementIsOpen ? '' + 'textGray icon-duigouzhuanhuan' : '' + 'textGreen icon-duigou'}` }
                           onClick={this.handleSwitchAgree}></i>
                        <span>我同意</span>
                        <a href="./ContractualProvision" className="textBlue">《快件运单契约条款》</a>

                    </p>
                </div>
                <div className="gray bottom_submit">
                    <div className="">
                        <p>
                            <span>费用估算： </span>
                            <span className="textRed">¥ {calc_fee}</span>
                        </p>

                    </div>
                    <div className=" yellow" onClick={this.handleSubmit}>提交</div>
                </div>

                <div>
                    <div className={maskClassName} id="iosMask" onClick={this.switchSiteList.bind(this, false)}></div>
                    <div className={actionSheetClassName} id="iosActionsheet">

                        {/*选择物品*/}
                        {this.state.goodsKindIsOpen && <div className="weui-cells">
                            <div className="weui-cell cancel">
                                <div className="weui-cell__bd">
                                    <p className="center"> 物品名称 </p>
                                </div>
                                <div className="weui-cell__ft" onClick={this.switchSiteList.bind(this, false)}>
                                    <i className="iconfont iconG icon-cha"></i>
                                </div>
                            </div>
                            <div className="weui-actionsheet__menu goods">
                                <span data-id="1"
                                      className={`${this.state.selectGoodName_id == 1 ? '' + ' bg_Y' : +'' + ' bg_G'}`}
                                      onClick={this.handleSelectGood.bind(this, "文件", 1)}> 文件 </span>

                                <span data-id="2"
                                      className={`${this.state.selectGoodName_id == 2 ? '' + ' bg_Y' : +'' + ' bg_G'}`}
                                      onClick={this.handleSelectGood.bind(this, "数码产品", 2)}> 数码产品 </span>

                                <span data-id="3"
                                      className={`${this.state.selectGoodName_id == 3 ? '' + ' bg_Y' : +'' + ' bg_G'}`}
                                      onClick={this.handleSelectGood.bind(this, "生活用品", 3)}> 生活用品 </span>

                                <span data-id="4"
                                      className={`${this.state.selectGoodName_id == 4 ? '' + ' bg_Y' : +'' + ' bg_G'}`}
                                      onClick={this.handleSelectGood.bind(this, "服饰", 4)}> 服饰 </span>

                                <span data-id="5"
                                      className={`${this.state.selectGoodName_id == 5 ? '' + ' bg_Y' : +'' + ' bg_G'}`}
                                      onClick={this.handleSelectGood.bind(this, "食品", 5)}> 食品 </span>

                                <span className={`${this.state.selectGoodName_id == 6 ? '' + ' bg_Y' : +'' + ' bg_G'}`}
                                      onClick={this.switchGoodsTextarea.bind(this, 6)}> 其他 </span>
                                <p className={goodsInputClassName}>
                                    <input type="text" name="" placeholder="请告我们您寄的是什么？"
                                           onChange={this.getInputValue.bind(this, 'selectGoodName')} id=""/>
                                    <span onClick={this.switchGoodsTextarea.bind(this, 6)}>确定</span>
                                </p>
                            </div>
                        </div>}

                        {/*选择物品重量*/}
                        {this.state.goodsWeightIsOpen && <div className="weui-cells">
                            <div className="weui-cell cancel">
                                <div className="weui-cell__bd">
                                    <p className="center"> 物品重量 </p>
                                </div>
                                <div className="weui-cell__ft" onClick={this.switchSiteList.bind(this, false)}>
                                    <i className="iconfont iconG icon-cha"></i>
                                </div>
                            </div>
                            <a className="weui-cell weui-cell_access fontSize12px select_good"
                               onClick={this.handleSelectGoodList}>
                                <div className="weui-cell__bd">
                                    <p>物品名称</p>
                                </div>
                                <div className="weui-cell__ft">{this.state.selectGoodName}</div>
                            </a>
                            <div className="weui-actionsheet__menu goods">
                                <span
                                    className={`${this.state.selectGoodWeight_id == 1 ? '' + ' bg_Y' : +'' + ' bg_G'}`}
                                    onClick={this.handleSelectWeight.bind(this, "1", 1)}> 1Kg </span>

                                <span
                                    className={`${this.state.selectGoodWeight_id == 2 ? '' + ' bg_Y' : +'' + ' bg_G'}`}
                                    onClick={this.handleSelectWeight.bind(this, "1.5", 2)}> 1.5Kg </span>

                                <span
                                    className={`${this.state.selectGoodWeight_id == 3 ? '' + ' bg_Y' : +'' + ' bg_G'}`}
                                    onClick={this.handleSelectWeight.bind(this, "2", 3)}> 2Kg </span>

                                <span
                                    className={`${this.state.selectGoodWeight_id == 4 ? '' + ' bg_Y' : +'' + ' bg_G'}`}
                                    onClick={this.handleSelectWeight.bind(this, "2.5", 4)}> 2.5Kg </span>

                                <span
                                    className={`${this.state.selectGoodWeight_id == 5 ? '' + ' bg_Y' : +'' + ' bg_G'}`}
                                    onClick={this.handleSelectWeight.bind(this, "3", 5)}> 3Kg </span>

                                <span
                                    className={`${this.state.selectGoodWeight_id == 6 ? '' + ' bg_Y' : +'' + ' bg_G'}`}
                                    onClick={this.switchWeightTextarea.bind(this, 6)}> 其他 </span>
                                <p className={weightInputClassName}>
                                    <input type="number" name="" ref="inputWeight"
                                           onChange={this.getInputValue.bind(this, 'selectGoodWeight')}
                                           placeholder="请告我们您的物品重量？             kg" id=""/>
                                    <span onClick={this.switchWeightTextarea.bind(this, 6)}>确定</span>
                                </p>
                            </div>

                        </div>}

                        {/*选择增值服务*/}
                        {this.state.addedServicesIsOpen && <div className="weui-cells">
                            <div className="weui-cell cancel">
                                <div className="weui-cell__bd">
                                    <p className="center"> 保价 </p>
                                </div>
                                <div className="weui-cell__ft" onClick={this.switchSiteList.bind(this, false)}>
                                    <i className="iconfont iconG icon-cha"></i>
                                </div>
                            </div>
                            <div className="weui-cell input_box">
                                <div className="input_box_left">
                                    <p className=""> 金额 </p>
                                </div>
                                <div className="input_box_right">
                                    <input type="number" name="" placeholder="请填写您的物品实际价值" id=""
                                           value={this.state.selectGoodPrice / 100}
                                           onChange={this.getExpenses}/>
                                    <span> 元</span>
                                </div>
                            </div>
                            <div className="weui-cell" style={{height: '.32rem', lineHeight: '.32rem'}}>
                                <div className="weui-cell__bd">
                                    <p className="fontSize12px">预计收取费用 <span
                                        className="colorRed">{this.state.insureFee / 100} 元</span></p>
                                </div>

                            </div>
                            <div className="weui-actionsheet__menu">
                                <p> 温馨提示： </p>
                                <p>1、预估收取费用仅供参考，请以服务网点收取为准。</p>
                                <p>2、物品价值不能>100万。</p>
                            </div>
                            <div className="weui-btn-area submit_btn">
                                <a className="weui-btn weui-btn_primary yellow" href="javascript:"
                                   id="showTooltips" onClick={this.handleConformInsureFee}>确认</a>
                            </div>
                        </div>}

                        {/*选择快递公司*/}
                        {this.state.selectDeliveryIsOpen && <div className="weui-cells">
                            <div className="weui-cell cancel">
                                <div className="weui-cell__bd">
                                    <p className="center">
                                    </p>
                                </div>
                                <div className="weui-cell__ft" onClick={this.switchSiteList.bind(this, false)}>
                                    <i className="iconfont iconG icon-cha"></i>
                                </div>
                            </div>
                            <a className="weui-cell weui-cell_access fontSize12px select_good">
                                <div className="weui-cell__bd">
                                    <p className="actionSheet_title">
                                        <span className={` ${this.state.sortby == 1 ? '' + 'textBlue' : ''}`}
                                              onClick={this.getOrderFeeList.bind(this, 1, 1)}>综合排序</span>
                                        <span className={` ${this.state.sortby == 2 ? '' + 'textBlue' : ''}`}
                                              onClick={this.getOrderFeeList.bind(this, 2, 1)}>价格</span>
                                        <span className={` ${this.state.sortby == 3 ? '' + 'textBlue' : ''}`}
                                              onClick={this.getOrderFeeList.bind(this, 3, 1)}>速度</span>
                                    </p>
                                </div>
                            </a>
                            <div style={{height: '2rem', overflow: 'scroll'}}>
                                <InfiniteScroll
                                    pageStart={0}
                                    threshold={5}
                                    loadMore={this.loadMoreData.bind(this, this.state.current)}
                                    hasMore={true}
                                    useWindow={false}
                                    loader={loader}
                                >
                                    <div className="weui-actionsheet__menu">
                                        {
                                            this.state.expressList.map(item => {
                                                return (
                                                    <div className="weui-cell delivery-item"
                                                         onClick={this.handleSelectExpress.bind(this, item)}>
                                                        <div className="weui-cell__hd">
                                                            <img src={item.img} alt=""
                                                                 style={{width: 40, marginRight: 5, display: "block"}}/>
                                                        </div>
                                                        <div className="weui-cell__bd">
                                                            <div>
                                                                <p className="flex">
                                                                    <span>{item.name}</span>
                                                                    <span>参考价：<span
                                                                        className="textRed">{item.amount / 100}</span>元</span>
                                                                </p>
                                                                <p className="fontSize12px flex textGray">
                                                                    <span>配送方式：{item.send_way_desc}</span>
                                                                    <span>预计送达时间： {item.max_arrive_time}天</span>
                                                                </p>
                                                            </div>
                                                        </div>
                                                    </div>
                                                )
                                            })
                                        }
                                    </div>

                                </InfiniteScroll>
                            </div>
                        </div>}

                        {/*选择付款方式*/}
                        {this.state.selectPaymentIsOpen && <div className="weui-cells">
                            <div className="weui-cell cancel">
                                <div className="weui-cell__bd">
                                    <p className="center"> 付款方式 </p>
                                </div>
                                <div className="weui-cell__ft" onClick={this.switchSiteList.bind(this, false)}>
                                    <i className="iconfont iconG icon-cha"></i>
                                </div>
                            </div>
                            <div className="weui-actionsheet__menu goods">
                                <span
                                    className={`${this.state.selectGoodPaymentId == 1 ? '' + ' bg_Y' : +'' + ' bg_G'}`}
                                    onClick={this.handleSelectPayType.bind(this, 1, "寄件人付")}> 寄件人付 </span>
                                <span
                                    className={`${this.state.selectGoodPaymentId == 2 ? '' + ' bg_Y' : +'' + ' bg_G'}`}
                                    onClick={this.handleSelectPayType.bind(this, 2, "收件人付")}> 收件人付 </span>
                                <span
                                    className={`${this.state.selectGoodPaymentId == 3 ? '' + ' bg_Y' : +'' + ' bg_G'}`}
                                    onClick={this.handleSelectPayType.bind(this, 3, "寄付月结")}> 寄付月结 </span>
                                <p className={paymentInputClassName}>
                                    <input type="text" name=""
                                           ref="myVipNum"
                                           onChange={this.getInputValue.bind(this, 'vipCode')}
                                           placeholder="请输入您的VIP账号             " id=""/>
                                    <span onClick={this.handleMatchVip}>确定</span>
                                </p>
                            </div>
                        </div>}

                    </div>
                </div>
            </div>
        )
    }
}
ReactDOM.render(<App />, document.getElementById('app'));
if (module.hot) {
    module.hot.accept();
}